using Comteck.Localization;
using DevExpress.Utils.Menu;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraPrinting;
using DevExpress.XtraTreeList.Nodes;
using DotNet.Dxperience.Dxperience.Localizer;
using System;
using System.Reflection;
using System.Windows.Forms;

namespace Comteck.Winforms.Utils {
  /// <summary>
  /// Dev帮助类
  /// </summary>
  public static partial class DevHelper {

    #region show message

    /// <summary>
    /// 
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    public static DialogResult ShowMsg(string text) {
      return XtraMessageBox.Show(text);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    public static DialogResult ShowMsg(string text, params object[] args) {
      var value = string.Format(text, args);
      return ShowMsg(value);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="message"></param>
    /// <returns></returns>
    public static DialogResult ShowMsg(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon) {
      return XtraMessageBox.Show(text, caption, buttons, icon);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="message"></param>
    /// <returns></returns>
    public static DialogResult ShowMsg(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton) {
      caption = caption ?? BaseResources.Default_Caption;
      return XtraMessageBox.Show(text, caption, buttons, icon, defaultButton);
    }

    #endregion

    #region 鼠标右键

    /// <summary>
    /// 鼠标右键
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public static void MyGridView_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e) {
      // 对象
      var gridView = sender as GridView;
      if (gridView == null) {
        return;
      }

      // 非自定义列
      if (e.HitInfo.InGroupPanel || e.HitInfo.InFilterPanel || e.MenuType == GridMenuType.Column) {
        return;
      }

      if (e.MenuType == GridMenuType.Row) {

        //// 保存布局
        //var menu_SaveLayout = new DXMenuItem("保存布局");
        //menu_SaveLayout.Image = Properties.Resources.copy_16x16;
        //e.Menu.Items.Add(menu_SaveLayout);

        //// 还原布局
        //var menu_RestoreLayout = new DXMenuItem("恢复原有样式");
        //menu_RestoreLayout.Image = Properties.Resources.copy_16x16;
        //e.Menu.Items.Add(menu_RestoreLayout);


        #region 复制

        // 复制
        var menu_copy = new DXSubMenuItem("复制");
        menu_copy.BeginGroup = true;
        menu_copy.Image = Properties.Resources.copy_16x16;

        // 复制单元格
        var menu_copy_cell = new DXMenuItem("单元格(&C)");
        menu_copy_cell.Image = Properties.Resources.copy_16x16;
        menu_copy_cell.Click += (x, y) =>
        {
          Clipboard.SetDataObject(gridView.GetFocusedRowCellDisplayText(gridView.FocusedColumn));
        };

        // 复制选中行
        var menu_copy_select = new DXMenuItem("选中行");
        menu_copy_select.Image = Properties.Resources.copy_16x16;
        menu_copy_select.Click += (x, y) =>
        {
          gridView.CopyToClipboard();
          //Clipboard.SetDataObject(gridView.GetSelectedCells());
        };

        // 复制全部数据
        var menu_copy_all = new DXMenuItem("全部数据");
        menu_copy_all.Image = Properties.Resources.copy_16x16;
        menu_copy_all.Click += (x, y) =>
        {
          var selectedRows = gridView.GetSelectedRows();

          string resultString = string.Empty;
          // gridView.BeginUpdate();
          gridView.SelectAll();
          var mi = typeof(DevExpress.XtraGrid.Views.Grid.GridView).GetMethod("GetText", BindingFlags.Instance | BindingFlags.NonPublic);
          resultString = mi.Invoke(gridView, null).ToString();
          gridView.ClearSelection();
          // gridView.EndUpdate();

          DataObject data = new DataObject();
          data.SetData(typeof(string), resultString);
          Clipboard.SetDataObject(data);

          foreach (var rowNum in selectedRows) {
            gridView.SelectRow(rowNum);
          }
        };

        menu_copy.Items.Add(menu_copy_cell);
        menu_copy.Items.Add(menu_copy_select);
        menu_copy.Items.Add(menu_copy_all);

        e.Menu.Items.Add(menu_copy);

        #endregion

        #region 导出

        // 导出
        var menu_export = new DXSubMenuItem("导出");
        menu_export.Image = Properties.Resources.export_16x16;
        //menu_export.Image = DevExpress.Images.ImageResourceCache.Default.GetImage("images/export/export_16x16.png");

        // 导出到 XLS
        var menu_export_XLS = new DXMenuItem("导出到 XLS");
        menu_export_XLS.Image = Properties.Resources.exporttoxls_16x16;
        menu_export_XLS.Click += (x, y) =>
        {
          gridView.ExportDialog(ExportTarget.Xls);
        };

        // 导出到 XLSX
        var menu_export_XLSX = new DXMenuItem("导出到 XLSX");
        menu_export_XLSX.Image = Properties.Resources.exporttoxlsx_16x16;
        menu_export_XLSX.Click += (x, y) =>
        {
          gridView.ExportDialog(ExportTarget.Xlsx);
        };

        // 导出到 XLSX
        var menu_export_CSV = new DXMenuItem("导出到 CSV");
        menu_export_CSV.Image = Properties.Resources.exporttoxlsx_16x16;
        menu_export_CSV.Click += (x, y) =>
        {
          gridView.ExportDialog(ExportTarget.Csv);
        };

        // 导出到 PDF
        var menu_export_PDF = new DXMenuItem("导出到 PDF");
        menu_export_PDF.Image = Properties.Resources.exporttopdf_16x16;
        menu_export_PDF.Click += (x, y) => {
          gridView.ExportDialog(ExportTarget.Pdf);
        };

        // 导出到 HTML
        var menu_export_HTML = new DXMenuItem("导出到 HTML");
        menu_export_HTML.Image = Properties.Resources.exporttohtml_16x16;
        menu_export_HTML.Click += (x, y) =>
        {
          gridView.ExportDialog(ExportTarget.Html);
        };
        menu_export_HTML.BeginGroup = true;

        // 导出到 Text
        var menu_export_TEXT = new DXMenuItem("导出到 Text");
        menu_export_TEXT.Image = Properties.Resources.exporttotxt_16x16;
        menu_export_TEXT.Click += (x, y) =>
        {
          gridView.ExportDialog(ExportTarget.Text);
        };

        // 导出到 MHT
        var menu_export_MHT = new DXMenuItem("导出到 MHT");
        menu_export_MHT.Image = Properties.Resources.exporttomht_16x16;
        menu_export_MHT.Click += (x, y) =>
        {
          gridView.ExportDialog(ExportTarget.Mht);
        };

        // 导出到 RTF
        var menu_export_RTF = new DXMenuItem("导出到 RTF");
        menu_export_RTF.Image = Properties.Resources.exporttortf_16x16;
        menu_export_RTF.Click += (x, y) =>
        {
          gridView.ExportDialog(ExportTarget.Rtf);
        };

        menu_export.Items.Add(menu_export_XLS);
        menu_export.Items.Add(menu_export_XLSX);
        menu_export.Items.Add(menu_export_CSV);
        //menu_export.Items.Add(menu_export_PDF);
        menu_export.Items.Add(menu_export_HTML);
        menu_export.Items.Add(menu_export_TEXT);
        menu_export.Items.Add(menu_export_MHT);
        menu_export.Items.Add(menu_export_RTF);

        e.Menu.Items.Add(menu_export);

        #endregion
      }
    }

    #endregion

    #region 设置时间

    /// <summary>
    /// 设置时间的走动
    /// </summary>
    /// <param name="control"></param>
    public static void SetFullControlTimeMove(BarItem control, string format = DateTimeFormat.LONGDATETIME) {
      // 秒表滴答滴答滴答
      bool tickTick = true;

      // 定时器
      System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
      timer.Interval = 500; // 每隔半秒钟,执行一次
      timer.Tick += (sender, e) => {
        if (tickTick) {
          control.Caption = DateTime.Now.ToString(format);
        } else {
          control.Caption = DateTime.Now.ToString(format);
        }
        tickTick = !tickTick;
      };

      // 定时器开启
      timer.Start();
    }

    #endregion

    #region 注册皮肤

    /// <summary>
    /// 注册皮肤
    /// </summary>
    public static void RegisterSkins() {
      DevExpress.UserSkins.BonusSkins.Register();
      DevExpress.Skins.SkinManager.EnableFormSkins();
      DevExpress.Skins.SkinManager.EnableMdiFormSkins();

      // 设置字体，默认为 tomaha
      //DevExpress.Utils.AppearanceObject.DefaultFont = new System.Drawing.Font("宋体", 9);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="skinName"></param>
    public static void SetSkin(string skinName) {
      //DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = SysGlobal.DevSkinName = skinName;
      DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = skinName;
    }

    #endregion

    #region Dev控件汉化

    /// <summary>
    /// Dev控件汉化
    /// </summary>
    public static void SetLocalizer() {
      //
      //DevExpress 控件本地化
      DevExpress.XtraEditors.Controls.Localizer.Active = new XtraEditorLocalizer();
      DevExpress.XtraBars.Localization.BarLocalizer.Active = new XtraBarLocalizer();
      DevExpress.XtraGrid.Localization.GridLocalizer.Active = new XtraGridLocalizer();
      DevExpress.XtraTreeList.Localization.TreeListLocalizer.Active = new XtraTreeListLocalizer();
      DevExpress.XtraNavBar.NavBarLocalizer.Active = new XtraNavBarLocalizer();
      DevExpress.XtraScheduler.Localization.SchedulerLocalizer.Active = new XtraSchedulerLocalizer();
      DevExpress.XtraPrinting.Localization.PreviewLocalizer.Active = new XtraPrintingPreviewLocalizer();
      DevExpress.XtraReports.Localization.ReportLocalizer.Active = new XtraReportLocalizer();
      DevExpress.XtraLayout.Localization.LayoutLocalizer.Active = new XtraLayoutLocalizer();
      DevExpress.XtraRichEdit.Localization.XtraRichEditLocalizer.Active = new XtraRichEditLocalizer();
    }

    #endregion

    #region dev多线程的皮肤和汉化

    /// <summary>
    /// dev针对多线程编程，需要单独设置皮肤和汉化信息，否则无效
    /// </summary>
    public static void SetThreadHack() {
      // 汉化
      SetLocalizer();

      // 设置皮肤
      SetSkin(SysGlobal.Settings.Dev_SkinName);
    }

    #endregion

    #region TreeList

    /// <summary>
    /// 选择子节点时触发
    /// </summary>
    /// <param name="node"></param>
    /// <param name="check"></param>
    /// <param name="action"></param>
    public static void SetCheckedChildNodes(TreeListNode node, CheckState check, Action<TreeListNode, CheckState> action = null) {
      for (var i = 0; i < node.Nodes.Count; i++) {
        node.Nodes[i].CheckState = check;
        SetCheckedChildNodes(node.Nodes[i], check, action);
        action?.Invoke(node.Nodes[i], check);
      }
    }

    /// <summary>
    /// 选择父节点时触发
    /// </summary>
    /// <param name="node"></param>
    /// <param name="check"></param>
    /// <param name="action"></param>
    public static void SetCheckedParentNodes(TreeListNode node, CheckState check, Action<TreeListNode, CheckState> action = null) {
      if (node.ParentNode != null) {
        var b = false;
        CheckState state;

        for (var i = 0; i < node.ParentNode.Nodes.Count; i++) {
          state = node.ParentNode.Nodes[i].CheckState;
          if (!check.Equals(state)) {
            b = !b;
            break;
          }
        }

        node.ParentNode.CheckState = b ? CheckState.Indeterminate : check;
        SetCheckedParentNodes(node.ParentNode, check, action);
        action?.Invoke(node.ParentNode, node.ParentNode.CheckState);
      }
    }

    #endregion

  }
}
